#pragma once
#include "KhaosPreHeaders.h"
#include "KhaosHandle.h"

namespace Khaos
{
    HandleObjectManager g_handleObjectManager;

    void HandleObjectManager::_clear()
    {
        for (Block* blk : m_blocks)
        {
            KHAOS_DELETE blk;
        }

        m_blocks.clear();
        m_currUsed = MaxBlockPtrCnt;
    }

    Handle HandleObjectManager::registerPointer(HandleObject* ptr)
    {
        if (m_currUsed == MaxBlockPtrCnt)
        {
            m_blocks.push_back(KHAOS_NEW Block);
            m_currUsed = 0;
        }

        {
            m_blocks.back()->ptrs[m_currUsed] = ptr;
            ++m_currUsed;
        }

        Handle handle = (m_blocks.size() - 1) * MaxBlockPtrCnt + m_currUsed;
        return handle;
    }

    void HandleObjectManager::unregisterPointer(HandleObject* ptr)
    {
        if (!ptr->getObjectHandle())
            return;

        _getValidPtrRef(ptr->getObjectHandle()) = nullptr;
    }
}

